package org.example.game.weekly.game441;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: TODO
 * @Author wyatt
 * @Data 2024/08/03 20:46
 */
public class Solution003 {

    public static void main(String[] args) {
        System.out.println(new Solution003().minZeroArray(new int[]{1,2,3,2,6}, new int[][]{{0,1,1},{0,2,1},{1,4,2},{4,4,4},{3,4,1},{4,4,5}}));
    }

    public static int minZeroArray(int[] nums, int[][] queries) {
        int sum = 0;
        int k = 0;

        Map<Integer, List<Integer>> indexValueMap = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            if(nums[i] > 0){
                sum++;
            }
            indexValueMap.putIfAbsent(i, new ArrayList<>());
            indexValueMap.get(i).add(nums[i]);
        }



        for (int i = 0; i < queries.length; i++) {
            for (int j = queries[i][0]; j <= queries[i][1]; j++) {
                if(nums[j] >= queries[i][2]){
                    List<Integer> values = indexValueMap.get(j);
                    if(values.isEmpty()){
                        continue;
                    }
                    for (int x = 0; x < values.size(); x++) {
                        if(values.get(x) == queries[i][2]){
                            sum--;
                            values = new ArrayList<>();
                            nums[j] = 0;
                            break;
                        }
                    }
                    List<Integer> addValues = new ArrayList<>();
                    for (int y = 0; y < values.size(); y++) {
                       if(values.get(y) > queries[i][2]){
                           addValues.add(values.get(y) - queries[i][2]);
                       }
                    }
                    values.addAll(addValues);
                }
            }
            k++;
            if(sum == 0) return k;
        }

        return -1;
    }
}
